package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.FacilityInfo;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author gqs
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
    implements ApartmentInfoService {
  @Autowired private ApartmentInfoMapper apartmentInfoMapper;
  @Autowired private LabelInfoMapper labelInfomapper;
  @Autowired private RoomInfoMapper roomInfoMapper;
  @Autowired private GraphInfoMapper graphInfoMapper;
  @Autowired private LabelInfoMapper labelInfoMapper;
  @Autowired private FacilityInfoMapper facilityInfoMapper;

  @Override
  public ApartmentItemVo selectApartmentItemById(Long apartmentId) {
    ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartmentById(apartmentId);

    List<LabelInfo> labelInfoList = labelInfomapper.selectListByApartmentId(apartmentId);

    List<GraphVo> graphVoList =
        graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, apartmentId);

    // 查询公寓最小租金
    BigDecimal minRent = roomInfoMapper.selectMinRentByApartmentId(apartmentId);

    ApartmentItemVo apartmentItemVo = new ApartmentItemVo();

    BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
    apartmentItemVo.setLabelInfoList(labelInfoList);
    apartmentItemVo.setGraphVoList(graphVoList);
    apartmentItemVo.setMinRent(minRent);

    return apartmentItemVo;
  }

  @Override
  public ApartmentDetailVo selectApartmentDetailById(Long id) {
    // 1.查询ApartmentInfo
    ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartmentById(id);
    if (apartmentInfo == null) {
      return null;
    }

    // 2.查询GraphInfo
    List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);

    // 3.查询LabelInfo
    List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);

    // 4.查询FacilityInfo
    List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);

    // 5.查询公寓最低房租
    BigDecimal minRent = roomInfoMapper.selectMinRentByApartmentId(id);

    ApartmentDetailVo appApartmentDetailVo = new ApartmentDetailVo();

    BeanUtils.copyProperties(apartmentInfo, appApartmentDetailVo);
    appApartmentDetailVo.setIsDeleted(apartmentInfo.getIsDeleted());
    appApartmentDetailVo.setGraphVoList(graphVoList);
    appApartmentDetailVo.setLabelInfoList(labelInfoList);
    appApartmentDetailVo.setFacilityInfoList(facilityInfoList);
    appApartmentDetailVo.setMinRent(minRent);
    return appApartmentDetailVo;
  }
}
